package com.ly.honor.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.honor.entity.*;
import com.ly.honor.excel.PlayerExportVO;
import com.ly.honor.excel.PlayerImportDTO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author 李某人
 * @Date 2023/3/19 20:56
 * @Version 1.0
 */
@Component
public class PlayerListener extends AnalysisEventListener<PlayerImportDTO> {

    @Autowired
    private PlayerMapper playerMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private CountryMapper countryMapper;

    @Autowired
    private EducationMapper educationMapper;

    @Autowired
    private PlayerPlaceMapper playerPlaceMapper;

    @Override
    public void invoke(PlayerImportDTO playerImportDTO, AnalysisContext analysisContext) {
        var player = new Player();

        var checkPlayer = playerMapper.selectOne(new LambdaQueryWrapper<Player>().eq(Player::getName,playerImportDTO.getName()));
        if(!ObjectUtils.isEmpty(checkPlayer)){
            throw new HonorException("该选手已存在");
        }
        player.setName(playerImportDTO.getName());
        player.setAge(playerImportDTO.getAge());
        player.setBiography(playerImportDTO.getBiography());
        player.setHonorIntegral(playerImportDTO.getHonorIntegral());

        var clueName = playerImportDTO.getClueName();
        var gameName = playerImportDTO.getGameName();
        var countryName = playerImportDTO.getCountryName();
        var educationName = playerImportDTO.getEducationName();
        var placeName = playerImportDTO.getPlaceName();
        if(clueName != null && clueName.trim() != ""){
            var clue = clueMapper.selectOne(new LambdaQueryWrapper<Clue>().eq(Clue::getName, clueName));
            if(ObjectUtils.isEmpty(clue)){
                throw new HonorException("文件中俱乐部不存在");
            }
            player.setClueId(clue.getId());
        }
        if(gameName != null && gameName.trim() != ""){
            var game = gameMapper.selectOne(new LambdaQueryWrapper<Game>().eq(Game::getName, gameName));
            if(ObjectUtils.isEmpty(game)){
                throw new HonorException("文件中游戏不存在");
            }
            player.setGameId(game.getId());
        }
        if(countryName != null && countryName.trim() != ""){
            var country = countryMapper.selectOne(new LambdaQueryWrapper<Country>().eq(Country::getName, countryName));
            if(ObjectUtils.isEmpty(country)){
                throw new HonorException("文件中国家不存在");
            }
            player.setCountryId(country.getId());
        }
        if(educationName != null && educationName.trim() != ""){
            var education = educationMapper.selectOne(new LambdaQueryWrapper<Education>().eq(Education::getName, educationName));
            if(ObjectUtils.isEmpty(education)){
                throw new HonorException("文件中学历不存在");
            }
            player.setEducationId(education.getId());
        }
        if(placeName != null && placeName.trim() != ""){
            var playerPlaceList = playerPlaceMapper.selectPlaceAll(player.getGameId());
            for (PlayerPlace playerPlace: playerPlaceList) {
                if(playerPlace.getName().equals(placeName)){
                    player.setPlaceId(playerPlace.getId());
                }
            }
            if(player.getPlaceId() == null){
                throw new HonorException("文件中位置不存在");
            }
        }
        //现役选手
        player.setRetireFlag(0);
        playerMapper.insert(player);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
